package cn.com.infinitus.uits.auto.uits;

//import net.yl.redis.MDB;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by R on 15/10/20.
 */
public class BaseDao
{
	
	
    private static SqlSessionFactory sqlSessionFactory;
    private SqlSession sqlSession ;
    private boolean autoCommit;
    
    public void openSession(){
		 sqlSession = sqlSessionFactory.openSession();
	}
    
    public void SetAutoCommit(boolean autoCommit){
    	this.autoCommit = autoCommit ;
    }
    
    public void closeSession(){
    	sqlSession.commit();
		sqlSession.close();
	}
    
    /**
     * 获取Session,该对象是session的代理对象，
     * 无需调用close关闭，否则会导致出错
     * @return Session
     */
    public  SqlSession getSession()
    {
        return sqlSession;
    }

    /**
     * 获取Mapper用于对DAO的操作
     * @param type 要获取的Mapper的class
     * @param <T> Mapper对象
     * @return  返回Mapper对象
     */
    public  <T> T getMapper(Class<T> type){ return getSession().getMapper(type); }

    /**
     * 查询
     * 优先通过内存去查找
     * @param id  myBatis映射文件对应的ID
     * @param params    参数
     * @return  查询结果
     */
    public  List<Map<String, Object>> query(String id, Object params,int needMdb){
        List<Map<String, Object>> result = null;
//        if(params instanceof Map){
//            result = ( List<Map<String, Object>>) MDB.init().getData(id,(Map) params) ;
//        }
//        if(result!=null){
//            return result ;
//        }
        result =  query(id, params) ;
//        MDB.init().addData(id,params,result);

        return result ;
    }

    /**
     * 查询
     * @param id  myBatis映射文件对应的ID
     * @param params    参数
     * @return  查询结果
     */
    public  List<Map<String, Object>> query(String id, Object params)
    {
        List<Map<String, Object>> record = new ArrayList<Map<String, Object>>();
        id = getId(id);
        params = params==null?new HashMap():params;
        record = sqlSession.selectList(id, params);
        if(autoCommit){
        	sqlSession.commit();
    		sqlSession.close();
    		sqlSession = sqlSessionFactory.openSession();
        }
        return record;
    }

    /**
     * 更新
     * @param id    myBatis映射文件对应的ID
     * @param params    参数
     * @return  影响的行数
     */
    public  int update(String id, Object params)
    {
        int effect = 0;
        id = getId(id);
        params = params==null?new HashMap():params;
        effect = sqlSession.update(id, params);
        if(autoCommit){
        	sqlSession.commit();
    		sqlSession.close();
    		sqlSession = sqlSessionFactory.openSession();
        }
        return effect;
    }

    /**
     * 批量查询
     * @param ids   myBatis映射文件对应的ID数组
     * @param params    参数
     * @return  结果
     */
    public List<List<Map<String, Object>>> querys(String[] ids, Object params)
    {
        List<List<Map<String, Object>>> records = new ArrayList<List<Map<String, Object>>>();
        List<Map<String, Object>> record = null;
        params = params==null?new HashMap():params;

        for(String id : ids)
        {
            id = getId(id);
            record = sqlSession.selectList(id, params);
            records.add(record);
        }
        if(autoCommit){
        	sqlSession.commit();
    		sqlSession.close();
    		sqlSession = sqlSessionFactory.openSession();
        }
        return records;
    }

    /**
     * 批量查询(多个查询参数结构不同时使用)
     * @param ids   myBatis映射文件对应的ID数组
     * @param params    参数
     * @return  结果
     */
    public List<List<Map<String, Object>>> querys(String[] ids, Object[] params)
    {
        List<List<Map<String, Object>>> records = new ArrayList<List<Map<String, Object>>>();
        List<Map<String, Object>> record = null;
        for(int i=0; i<ids.length; i++)
        {
            ids[i] = getId(ids[i]);
            record = sqlSession.selectList(ids[i], params[i]);
            records.add(record);
        }
        if(autoCommit){
        	sqlSession.commit();
    		sqlSession.close();
    		sqlSession = sqlSessionFactory.openSession();
        }

        return records;
    }

    /**
     * 批量更新
     * @param ids
     * @param params
     * @return
     */
    public int[] updates(String[] ids, Object params)
    {
        int[] effects = new int[ids.length];
        int effect = 0;
        SqlSession sqlSession = sqlSessionFactory.openSession();
        params = params==null?new HashMap():params;
        int i =0;
        for(String id : ids)
        {
            id = getId(id);
            effect = sqlSession.update(id, params);
            sqlSession.commit();
            effects[i++] = effect;
        }

        if(autoCommit){
        	sqlSession.commit();
    		sqlSession.close();
    		sqlSession = sqlSessionFactory.openSession();
        }
        return effects;
    }

    /**
     * 批量更新(多个更新参数结构不同时使用)
     * @param ids
     * @param params
     * @return
     */
    public int[] updates(String[] ids, Object[] params)
    {
        int[] effects = new int[ids.length];
        int effect = 0;
        int i =0;
        for(int o=0; o<ids.length; o++)
        {
            ids[o] = getId(ids[o]);
            effect = sqlSession.update(ids[o], params[o]);
            sqlSession.commit();
            effects[i++] = effect;
        }
        
        if(autoCommit){
        	sqlSession.commit();
    		sqlSession.close();
    		sqlSession = sqlSessionFactory.openSession();
        }

        return effects;
    }


    /**
     * 执行存储过程
     * @param id
     * @param params
     * @return
     */
    public List<List<Map<String, Object>>> exec(String id, Object params)
    {
        List<List<Map<String, Object>>> datas = new ArrayList<List<Map<String, Object>>>();
        id = getId(id);
        params = params==null?new HashMap():params;
        datas = sqlSession.selectList(id, params);
        
        if(autoCommit){
        	sqlSession.commit();
    		sqlSession.close();
    		sqlSession = sqlSessionFactory.openSession();
        }
        
        return datas;
    }

    /**
     * 为了提高开发效率，当没有填写全路径时，采用默认namespace
     * @param id mybatis映射id
     * @return 真正的id
     */
    private static String getId(String id)
    {
        id = id.indexOf(".") == -1?"default." + id:id;
        return id;
    }

    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        sqlSession = sqlSession;
    }
}
